package com.example.mall.user.svc.domain.model.entity;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.mall.common.ddd.AbstractEntity;
import com.example.mall.user.api.dto.UserDTO;
import com.example.mall.user.svc.app.cmd.BindCmd;
import com.example.mall.user.svc.domain.model.vo.LoginMode;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.Instant;
import java.util.List;

/**
 * @author Mickey
 * @since 2022/4/15
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@TableName(value = "pm_user", autoResultMap = true)
public class User extends AbstractEntity {

    private String mobile;
    private String avatar;
    private String username;
    private String password;
    private String nickname;
    private Instant birthday;
    @TableField(typeHandler = LoginMode.LoginModeArrayType.class)
    private List<LoginMode> loginModes;

    public void bind(BindCmd<?> bindCmd) {
        bindCmd.getBindCmdHandler()
                .getBindHandler()
                .accept(this, bindCmd, SpringUtil.getBean(bindCmd.getType().getName()));
    }

    public UserDTO converterToUserDTO() {
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(this.getUsername());
        userDTO.setPassword(this.getPassword());
        return userDTO;
    }
}
